package com.lushuai.jiubeidanci.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lushuai.jiubeidanci.db.DBHelper;
import com.lushuai.jiubeidanci.model.User;

public class UserDao {
    private DBHelper dbHelper;
    private SQLiteDatabase db;

    public UserDao(Context context) {
        dbHelper = new DBHelper(context);
    }

    // 插入新用户
    public long insert(User user) {
        db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_NICKNAME, user.getNickname());
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword());
        values.put(DBHelper.COLUMN_CREATE_TIME, user.getCreateTime());
        
        long id = db.insert(DBHelper.TABLE_USERS, null, values);
        db.close();
        return id;
    }

    // 更新用户信息
    public int update(User user) {
         db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        // 确保更新所有可能修改的字段
        values.put(DBHelper.COLUMN_NICKNAME, user.getNickname());
        values.put(DBHelper.COLUMN_PASSWORD, user.getPassword()); // 添加密码字段的更新
        values.put(DBHelper.COLUMN_AVATAR_URL, user.getAvatarUrl());
        values.put(DBHelper.COLUMN_IS_VIP, user.isVip() ? 1 : 0);
        values.put(DBHelper.COLUMN_COINS, user.getCoins());
        values.put(DBHelper.COLUMN_EQUIPMENT_COUNT, user.getEquipmentCount());
        values.put(DBHelper.COLUMN_EMAIL, user.getEmail());
        values.put(DBHelper.COLUMN_PHONE, user.getPhone());
        values.put(DBHelper.COLUMN_LAST_LOGIN_TIME, user.getLastLoginTime());
        int rows = db.update(DBHelper.TABLE_USERS, values,
                DBHelper.COLUMN_USER_ID + "=?",
                new String[]{String.valueOf(user.getUserId())});
        db.close();
        return rows;
    }

    // 根据用户名和密码查询用户（登录）
    public User login(String nickname, String password) {
        db = dbHelper.getReadableDatabase();
        User user = null;
        
        Cursor cursor = db.query(DBHelper.TABLE_USERS, null,
                DBHelper.COLUMN_NICKNAME + "=? AND " + DBHelper.COLUMN_PASSWORD + "=?",
                new String[]{nickname, password}, null, null, null);
                
        if (cursor != null && cursor.moveToFirst()) {
            user = cursorToUser(cursor);
            cursor.close();
        }
        db.close();
        return user;
    }

    // 根据ID查询用户
    public User getUserById(long userId) {
        db = dbHelper.getReadableDatabase();
        User user = null;
        
        Cursor cursor = db.query(DBHelper.TABLE_USERS, null,
                DBHelper.COLUMN_USER_ID + "=?",
                new String[]{String.valueOf(userId)}, null, null, null);
                
        if (cursor != null && cursor.moveToFirst()) {
            user = cursorToUser(cursor);
            cursor.close();
        }
        db.close();
        return user;
    }

    // 将Cursor转换为User对象
    private User cursorToUser(Cursor cursor) {
        User user = new User();
        user.setUserId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_USER_ID)));
        user.setNickname(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NICKNAME)));
        user.setPassword(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PASSWORD)));
        user.setAvatarUrl(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_AVATAR_URL)));
        user.setVip(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_IS_VIP)) == 1);
        user.setCoins(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_COINS)));
        user.setEquipmentCount(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_EQUIPMENT_COUNT)));
        user.setEmail(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_EMAIL)));
        user.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PHONE)));
        user.setCreateTime(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_CREATE_TIME)));
        user.setLastLoginTime(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_LAST_LOGIN_TIME)));
        return user;
    }
} 